package com.petcommunity.dao;

import com.petcommunity.model.Pet;
import com.petcommunity.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class PetDAO {

    public void addPet(Pet pet) {
        String sql = "INSERT INTO pets (name, species, breed, gender, birth_date, owner_id) VALUES (?, ?, ?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, pet.getName());
            stmt.setString(2, pet.getSpecies());
            stmt.setString(3, pet.getBreed());
            stmt.setString(4, pet.getGender());
            stmt.setDate(5, new java.sql.Date(pet.getBirthDate().getTime()));
            stmt.setInt(6, pet.getOwnerId());
            stmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<Pet> getAllPets() {
        List<Pet> list = new ArrayList<>();
        String sql = "SELECT * FROM pets ORDER BY pet_id ASC";
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                Pet pet = new Pet();
                pet.setPetId(rs.getInt("pet_id"));
                pet.setName(rs.getString("name"));
                pet.setSpecies(rs.getString("species"));
                pet.setBreed(rs.getString("breed"));
                pet.setGender(rs.getString("gender"));
                pet.setBirthDate(rs.getDate("birth_date"));
                pet.setOwnerId(rs.getInt("owner_id"));
                list.add(pet);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    public Pet getPetById(int id) {
        Pet pet = null;
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "SELECT * FROM pets WHERE pet_id = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                pet = new Pet();
                pet.setPetId(rs.getInt("pet_id"));
                pet.setName(rs.getString("name"));
                pet.setSpecies(rs.getString("species"));
                pet.setBreed(rs.getString("breed"));
                pet.setGender(rs.getString("gender"));
                pet.setBirthDate(rs.getDate("birth_date"));
                pet.setOwnerId(rs.getInt("owner_id"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pet;
    }

    public void updatePet(Pet pet) {
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "UPDATE pets SET name=?, species=?, breed=?, gender=?, birth_date=?, owner_id=? WHERE pet_id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, pet.getName());
            ps.setString(2, pet.getSpecies());
            ps.setString(3, pet.getBreed());
            ps.setString(4, pet.getGender());
            ps.setDate(5, (Date) pet.getBirthDate());
            ps.setInt(6, pet.getOwnerId());
            ps.setInt(7, pet.getPetId());
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deletePet(int id) {
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "DELETE FROM pets WHERE pet_id = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

